package com.ecommerce.payment.controller;

import com.ecommerce.common.core.ApiResult;
import com.ecommerce.payment.dto.*;
import com.ecommerce.payment.service.PaymentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

/**
 * 支付管理控制器
 * 
 * @author ecommerce
 * @since 1.0.0
 */
@RestController
@RequestMapping("/api/v1/payments")
@RequiredArgsConstructor
@Tag(name = "支付管理", description = "支付相关的API接口")
public class PaymentController {

    private final PaymentService paymentService;

    /**
     * 创建支付订单
     */
    @PostMapping
    @Operation(summary = "创建支付订单", description = "创建新的支付订单")
    @ApiResponse(responseCode = "201", description = "创建成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = PaymentResponse.class)))
    public ApiResult<PaymentResponse> createPayment(
            @Parameter(description = "支付创建请求", required = true)
            @RequestBody CreatePaymentRequest request) {
        return paymentService.createPayment(request);
    }

    /**
     * 获取支付订单详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取支付订单详情", description = "根据支付订单ID获取详细信息")
    @ApiResponse(responseCode = "200", description = "获取成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = PaymentResponse.class)))
    public ApiResult<PaymentResponse> getPaymentById(
            @Parameter(description = "支付订单ID", required = true)
            @PathVariable Long id) {
        // 实际应该根据ID查询，这里使用支付单号查询
        return paymentService.queryPaymentStatus("PAY" + id);
    }

    /**
     * 获取支付订单列表
     */
    @GetMapping
    @Operation(summary = "获取支付订单列表", description = "分页获取支付订单列表")
    @ApiResponse(responseCode = "200", description = "获取成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = PaymentResponse.class)))
    public ApiResult<Page<PaymentResponse>> getPayments(
            @Parameter(description = "订单ID")
            @RequestParam(required = false) Long orderId,
            @Parameter(description = "支付状态")
            @RequestParam(required = false) String status,
            @PageableDefault(size = 10) Pageable pageable) {
        // 简化实现：返回空列表，实际应该根据条件查询
        return ApiResult.success(Page.empty());
    }

    /**
     * 支付回调
     */
    @PostMapping("/callback")
    @Operation(summary = "支付回调", description = "处理第三方支付平台的回调通知")
    @ApiResponse(responseCode = "200", description = "处理成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = PaymentResponse.class)))
    public ApiResult<PaymentResponse> handlePaymentCallback(
            @Parameter(description = "支付回调请求", required = true)
            @RequestBody PaymentCallbackRequest request) {
        return paymentService.handlePaymentCallback(request);
    }

    /**
     * 申请退款
     */
    @PostMapping("/{id}/refund")
    @Operation(summary = "申请退款", description = "申请订单退款")
    @ApiResponse(responseCode = "200", description = "申请成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = RefundResponse.class)))
    public ApiResult<RefundResponse> refundPayment(
            @Parameter(description = "支付订单ID", required = true)
            @PathVariable Long id,
            @Parameter(description = "退款请求", required = true)
            @RequestBody RefundRequest request) {
        return paymentService.requestRefund(request);
    }

    /**
     * 获取退款详情
     */
    @GetMapping("/{id}/refund")
    @Operation(summary = "获取退款详情", description = "获取支付订单的退款详情")
    @ApiResponse(responseCode = "200", description = "获取成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = RefundResponse.class)))
    public ApiResult<RefundResponse> getRefundInfo(
            @Parameter(description = "支付订单ID", required = true)
            @PathVariable Long id) {
        // 简化实现：使用退款单号查询
        return paymentService.queryRefundStatus("REFUND" + id);
    }
}